package forBFK;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonObject;
import com.honeymoondesert.util.JsonUtil;
import com.honeymoondesert.util.MapUtil;
import com.honeymoondesert.util.PropertyHelper;

public class BaiduDistanceCSV {
	public static List<JsonObject> getSurrounds(String parentID){
		 List<JsonObject> surrounds=new ArrayList<JsonObject>();
		 PropertyHelper ph=new PropertyHelper(null);
		 String surroundPath=ph.getProperties("output")+"surround\\"+parentID;
		 System.out.println(surroundPath);
		 File dir=new File(surroundPath);
		 for (File file : dir.listFiles()) {
			JsonObject sJSON=new JsonUtil().traslateToJson(file.getAbsolutePath());
			surrounds.add(sJSON);
		}
		
		return surrounds;
	}
	public static void main(String[] args) {
		MapUtil mu=new MapUtil();
		
		PropertyHelper ph=new PropertyHelper(null);
		String basestorePath=ph.getProperties("output")+"baseStore";
		File dir=new File(basestorePath);
		FileOutputStream fos=null;
		try {
			fos=new FileOutputStream(new File(ph.getProperties("output")+"need.csv"));
			for (File file:dir.listFiles()) {
				JsonObject bsJSON=new JsonUtil().traslateToJson(file.getAbsolutePath());
				List<JsonObject> surrounds=BaiduDistanceCSV.getSurrounds(bsJSON.get("id").getAsString());

				fos.write("店名,区,地址,电话,评论数,评分,5/4/3/2/1星,均价,口味,环境,服务,日期,点评地址,邻属满记,相距(m)\n\r".getBytes());
				String output=(bsJSON.get("storeName")==null?"":bsJSON.get("storeName"))+","
						+(bsJSON.get("district")==null?"":bsJSON.get("district"))+","
						+(bsJSON.get("address")==null?"":bsJSON.get("address"))+","
						+(bsJSON.get("tel")==null?"":bsJSON.get("tel"))+","
						+(bsJSON.get("commentNum")==null?"":bsJSON.get("commentNum"))+","
						+(bsJSON.get("score")==null?"":bsJSON.get("score"))+","
						+(bsJSON.get("distribution")==null?"":bsJSON.get("distribution"))+","
						+(bsJSON.get("advPrice")==null?"":bsJSON.get("advPrice"))+","
						+(bsJSON.get("taste")==null?"":bsJSON.get("taste"))+","
						+(bsJSON.get("enviroment")==null?"":bsJSON.get("enviroment"))+","
						+(bsJSON.get("service")==null?"":bsJSON.get("service"))+","
						+(bsJSON.get("date")==null?"":bsJSON.get("date"))+","
						+(bsJSON.get("storeUrl")==null?"":bsJSON.get("storeUrl"))+","
						+"满记店"+","
						+""
						+"\n\r"
						;
				fos.write(output.getBytes());
				for (JsonObject jo : surrounds) {
					String baseAdd=bsJSON.get("address").getAsString();
					String joAdd=jo.get("address").getAsString();
					Double distance=null;
					try {
						distance=mu.getDistance(MapUtil.removeSign(baseAdd), MapUtil.removeSign(joAdd), "上海", "上海");
					} catch (Exception e) {
						// TODO: handle exception
					}
					String op=jo.get("storeName")+","
							+jo.get("district")+","
							+jo.get("address")+","
							+jo.get("tel")+","
							+jo.get("commentNum")+","
							+jo.get("score")+","
							+jo.get("distribution")+","
							+jo.get("advPrice")+","
							+jo.get("taste")+","
							+jo.get("enviroment")+","
							+jo.get("service")+","
							+jo.get("date")+","
							+jo.get("storeUrl")+","
							+bsJSON.get("storeName")+","
							+(distance==null?"无法获取坐标":distance.toString())
							+"\n\r"
							;
					fos.write(op.getBytes());
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	
}
